package com.liang.text;


import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;

/*
*
* */
public class BirthdayDemo {
    public static void main(String[] args) {
        //通过键盘输入一个生日
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您的出生年月");
        String s = sc.nextLine();

        DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd");


        //根据生日计算年龄
        LocalDate birth = LocalDate.parse(s,format);

        method(birth);

    }

    public static void method(LocalDate birth) {
        LocalDate now = LocalDate.now();
        int birthYear = birth.getYear();
        int nowYear = now.getYear();
        int birthMonth = birth.getMonthValue();
        int nowMonth = now.getMonthValue();
        int birthDay = birth.getDayOfMonth();
        int nowDay = now.getDayOfMonth();

        int age = nowYear - birthYear;

        if (birthMonth > nowMonth) {
            age--;

        } else if (birthMonth == nowMonth) {
            if (birthDay > nowDay) {
                age--;
            }
        }
        System.out.println("您的年龄为"+age);
    }
}
